package com.example.lanouhn.bantangapp.activities.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.contants.DT_Details;

import java.io.IOException;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Community  文章里的item
 * Created by lanouhn on 16/7/28.
 */
public class Community_Artical_Item_Activity extends Activity {

    //接收到的id
    private int id;
    private WebView wv_webview;

    private ImageView back;//返回
    private ImageView iv_share;//分享
    private TextView tv_say;// 我来说两句

    private ImageView iv_collection;//收藏 弹窗
    private ImageView iv_aiready_collection;//已收藏 弹窗
    private LinearLayout lly_collection;//收藏
    private LinearLayout lly_aiready_collection;//已收藏


    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    return;
                case 1:
                    String net = (String) msg.obj;
                    WebSettings webSettings = wv_webview.getSettings();
                    //设置支持js方法
                    webSettings.setJavaScriptEnabled(true);
                    //支持自动加载图片
                    webSettings.setLoadsImagesAutomatically(true);
                    wv_webview.loadUrl(net);

                    //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
                    wv_webview.setWebViewClient(new WebViewClient() {
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                            view.loadUrl(url);
                            return true;
                        }
                    });
                    break;
                case 2:
                    iv_collection.setVisibility(View.GONE);
                    iv_aiready_collection.setVisibility(View.GONE);
                    break;


            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.community_artical_item_activity);

        initView();
        initListener();

        //分享
        ShareSDK.initSDK(this, "sharesdk的appkey");

        Intent intent = getIntent();
        //接收id，第二个参数：如果没有id，默认为0
        id = intent.getIntExtra("id", 0);

        getDataFromNet();
    }


    /**
     * 初始化控件
     */
    private void initView() {
        wv_webview = (WebView) findViewById(R.id.wv_webview);
        back = (ImageView) findViewById(R.id.back);
        iv_share = (ImageView) findViewById(R.id.iv_share);
        tv_say = (TextView) findViewById(R.id.tv_say);
        lly_collection = (LinearLayout) findViewById(R.id.lly_collection);
        lly_aiready_collection = (LinearLayout) findViewById(R.id.lly_aiready_collection);

        iv_collection = (ImageView) findViewById(R.id.iv_collection);
        iv_aiready_collection = (ImageView) findViewById(R.id.iv_aiready_collection);
    }

    /**
     * 监听事件
     */
    private void initListener() {
        //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //分享
        iv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShare();
            }
        });

        //收藏
        lly_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击显示   收藏图片
                lly_collection.setVisibility(View.INVISIBLE);
                lly_aiready_collection.setVisibility(View.VISIBLE);
                iv_collection.setVisibility(View.INVISIBLE);
                iv_aiready_collection.setVisibility(View.VISIBLE);


                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            Thread.sleep(2000);

                            handler.sendEmptyMessage(2);


                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });
        //已收藏
        lly_aiready_collection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击显示   已收藏图片并关闭收藏图片
                lly_collection.setVisibility(View.VISIBLE);
                lly_aiready_collection.setVisibility(View.INVISIBLE);
                iv_collection.setVisibility(View.VISIBLE);
                iv_aiready_collection.setVisibility(View.INVISIBLE);
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            Thread.sleep(1000);

                            handler.sendEmptyMessage(2);


                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();


            }
        });
        //输入框 说两句 的 跳转
        tv_say.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(Community_Artical_Item_Activity.this, Details_Comment_Activity.class);
                startActivity(it);
            }
        });

    }

    /**
     * 请求网络链接
     */
    public void getDataFromNet() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //拼接网址
                final String url = DT_Details.DETAILS + "id=" + id;

                OkHttpClient client = new OkHttpClient();

                Request request = new Request.Builder()
                        .url(url)
                        .build();

                //回调
                Call call = client.newCall(request);
                call.enqueue(new Callback() {

                    //回调失败
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                        e.printStackTrace();
                    }

                    //回调成功
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Message message = handler.obtainMessage();
                        message.obj = url;
                        message.what = 1;
                        handler.sendMessage(message);
                    }
                });
            }
        }).start();
    }
    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }
}
